import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_file_upload_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/violation_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/management_operation_page/violation_records/violation_records_controller.dart';

/// 违规登记
class ViolationRecordsPage extends StatelessWidget {
  ViolationRecordsPage({super.key});

  final controller = Get.put(ViolationRecordsController());

  /// 类型、理由
  Widget _getTypeContentView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      margin: EdgeInsets.only(top: 20.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              children: [
                Text(
                  '违规类型',
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    fontWeight: FontWeight.w500,
                    color: AppColors.font_color_black,
                  ),
                ),
                AppGaps.hGap6,
                Expanded(
                    child: Container(
                  alignment: Alignment.centerRight,
                  child: Text(
                    controller.violationEntity.violationTypeName!.isNotEmpty
                        ? controller.violationEntity.violationTypeName!
                        : '请选择',
                    style: TextStyle(
                      fontSize: 14.sp,
                      height: 1.2,
                      color: controller.violationEntity.violationTypeName!.isNotEmpty
                          ? const Color(0xFF262F38)
                          : AppColors.text_grey,
                    ),
                  ),
                )),
                AppGaps.hGap6,
                Icon(Icons.arrow_forward_ios, size: 14.sp, color: AppColors.text_grey)
              ],
            ),
          ).clickOnce(controller.selectViolationType),
          Divider(
            height: 1.w,
            color: AppColors.bg_color_grey,
          ),
          AppGaps.vGap20,
          Text(
            '违规理由',
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w500,
              color: AppColors.font_color_black,
            ),
          ),
          AppGaps.vGap16,
          Container(
              height: 135.w,
              decoration: BoxDecoration(
                color: const Color(0xffF7F7FB),
                borderRadius: BorderRadius.all(Radius.circular(4.w)),
                border: Border.all(width: 1.w, color: const Color(0xffEBEDF0)),
              ),
              child: TextField(
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.done,
                  maxLines: null,
                  onChanged: (value) {
                    controller.violationEntity.violationReason = value;
                  },
                  style:
                      TextStyle(color: const Color(0xFF262F38), fontWeight: FontWeight.w400, fontSize: 14.w),
                  decoration: InputDecoration(
                    counterText: '',
                    //防止适配不居中
                    isCollapsed: true,
                    border: const OutlineInputBorder(borderSide: BorderSide.none),
                    contentPadding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 9.w),
                    hintText: '请输入违规理由...',
                    hintStyle: TextStyle(
                        fontSize: 14.w, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                  ))),
          AppGaps.vGap20
        ],
      ),
    );
  }

  ///获取回执列表
  Widget _getReceiptListView(ViolationViolationListDTOList item, int index) {
    return Container(
      padding: EdgeInsets.only(left: 12.w, top: 12.w, right: 12.w),
      decoration: BoxDecoration(
        color: const Color(0xFFFAFAFA),
        borderRadius: BorderRadius.circular(8.w),
      ),
      alignment: Alignment.center,
      child: Column(
        children: [
          Container(
              height: 80.w,
              decoration: BoxDecoration(
                color: const Color(0xffF7F7FB),
                borderRadius: BorderRadius.all(Radius.circular(4.w)),
                border: Border.all(width: 1.w, color: const Color(0xffEBEDF0)),
              ),
              child: TextField(
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.done,
                  controller: TextEditingController(text: item.violationCircle),
                  maxLines: null,
                  onChanged: (value) {
                    item.violationCircle = value;
                  },
                  style:
                      TextStyle(color: const Color(0xFF262F38), fontWeight: FontWeight.w400, fontSize: 14.w),
                  decoration: InputDecoration(
                    counterText: '',
                    //防止适配不居中
                    isCollapsed: true,
                    border: const OutlineInputBorder(borderSide: BorderSide.none),
                    contentPadding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 9.w),
                    hintText: '请输入...',
                    hintStyle: TextStyle(
                        fontSize: 14.w, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                  ))),
          CustomFileUploadPage(
            isRequired: false,
            isShowDivider: false,
            uploadedFiles: item.violationCircleFile,
            title: '上传附件',
            onCallBack: (value) {
              item.violationCircleFile = value;
            },
          ),
        ],
      ),
    );
  }

  /// 回执
  Widget _getReceiptView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.topLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      child: Column(
        children: [
          Row(
            children: [
              Text(
                '回执',
                style: TextStyle(
                  fontSize: 14.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w500,
                  color: AppColors.font_color_black,
                ),
              ),
              const Spacer(),
              Text(
                '添加回执',
                style: TextStyle(
                    color: const Color(0xff5C6DFF),
                    height: 1.2,
                    fontWeight: FontWeight.w400,
                    fontSize: 12.sp),
              ).clickOnce(controller.addReceipt),
            ],
          ),
          AppGaps.vGap16,
          ListView.separated(
            itemCount: controller.receiptList.length,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              final item = controller.receiptList[index];
              return Container(
                alignment: Alignment.center,
                child: Stack(
                  alignment: Alignment.topRight,
                  children: [
                    //获取回执列表
                    _getReceiptListView(item, index),
                    //删除按钮
                    Visibility(
                        visible: index != 0,
                        child: const Icon(
                          Icons.highlight_remove,
                          color: Color(0xFFFD6B6B),
                        ).clickOnce(() {
                          controller.removeReceipt(item);
                        }))
                  ],
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return AppGaps.vGap8;
            },
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '违规登记',
      ),
      body: GetBuilder(
          init: controller,
          builder: (_) {
            return SingleChildScrollView(
              child: Column(
                children: [
                  // 类型、理由选择
                  _getTypeContentView(),
                  // 回执
                  _getReceiptView(),
                  AppGaps.vGap60,
                ],
              ),
            );
          }),
      bottomSheet: Container(
        height: 50.w,
        width: double.infinity,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            backgroundColor: AppColors.blue_button,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(22.r),
            ),
          ),
          onPressed: controller.complete,
          child: Text(
            '确定',
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
